set -e
show_usage() {
  echo "usage:
      -a is android api number
      -b is abi (armeabi-v7a、arm64-v8a、x86、x86_64)  
      -f is ffmpeg version
      -h is help
   "
}
#ffmpeg版本 -可配置
FFMPEG_VERSION=4.4.4
#ndk api版本 -可配置
API=24
#abi  -可配置 ：armeabi-v7a、 、x86、x86_64
ABI=armeabi-v8a
while getopts "f:a:b:h" arg; do
  case $arg in
  b)
    ABI=$OPTARG
    ;;
  a)
    API=$OPTARG
    ;;
  f)
    FFMPEG_VERSION=$OPTARG
    ;;
  h)
    show_usage
    exit 1
    ;;
  ?)
    echo "unkonw argument"
    show_usage
    exit 1
    ;;
  esac
done
OS=$(uname -o)
echo $OS
if [ $OS == "Msys" ]; then
  OS="windows"
  NDK_URL="https://googledownloads.cn/android/repository/android-ndk-r26d-windows.zip"
  NDK_SHA1="c7ea35ffe916082876611da1a6d5618d15430c29"
else
  OS="linux"
  NDK_URL=" https://googledownloads.cn/android/repository/android-ndk-r26d-linux.zip"
  NDK_SHA1="fcdad75a765a46a9cf6560353f480db251d14765"

fi
echo "************************************Build Info*********************************"
echo "Host os :$OS"
echo "Android api :$API"
echo "Target abi :$ABI"
echo "FFmpeg version :$FFMPEG_VERSION"
echo "************************************Build Info End*****************************"
PACKAGE_SOURCE_DIR=$(pwd)/.package
#下载NDK
if [ ! -d "$PACKAGE_SOURCE_DIR" ]; then
  mkdir $PACKAGE_SOURCE_DIR
fi
if [ ! -d "$ANDROID_NDK_HOME" ]; then
  pushd $PACKAGE_SOURCE_DIR
  if [ ! -d "ndk" ]; then
    if [ -e "ndk.zip" ]; then
      sha1=$(sha1sum ndk.zip)
    fi
    sha1=($sha1)
    echo "${sha1[0]}"
    if [ "$sha1" != "$NDK_SHA1" ]; then
      if [ -e "ndk.zip" ]; then
        rm ndk.zip
      fi
      echo ndk Downloading...
      wget $NDK_URL -O ndk.zip
    fi
    if [ -d "ndk" ]; then
      rm -rf ndk
    fi
    unzip ndk.zip -d ndk
  fi
  ANDROID_NDK_HOME=$PACKAGE_SOURCE_DIR/ndk/$(ls ndk)
  popd
fi
#下载NDK -end
TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$OS-x86_64
echo $TOOLCHAIN
SYSROOT=$TOOLCHAIN/sysroot
case $ABI in
armeabi-v7a)
  CPU=armv7-a
  ARCH=arm
  CROSS_PREFIX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-
  USR_LIB=$SYSROOT/usr/lib/armv7a-linux-androideabi/$API
  ;;
arm64-v8a)
  CPU=armv8-a
  ARCH=arm64
  CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android$API-
  USR_LIB=$SYSROOT/usr/lib/aarch64-linux-android/$API
  ;;
x86)
  CPU=i686
  ARCH=x86
  CROSS_PREFIX=$TOOLCHAIN/bin/i686-linux-android$API-
  USR_LIB=$SYSROOT/usr/lib/i686-linux-android/$API
  FFMPEG_EXTRA_CONFIGURE_FLAGS=--disable-asm
  ;;
x86_64)
  CPU=x86-64
  ARCH=x86_64
  CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android$API-
  USR_LIB=$SYSROOT/usr/lib/x86_64-linux-android/$API
  ;;
esac
PREFIX=$(pwd)/output/ffmpeg${FFMPEG_VERSION}/${API}/${ABI}
PACKAGE_PREFIX=$PACKAGE_SOURCE_DIR/.output/${API}/${ABI}
export CC=${CROSS_PREFIX}clang
export CXX=${CROSS_PREFIX}clang++
CROSS_PREFIX=$TOOLCHAIN/bin/llvm-
##x264
#pushd $PACKAGE_SOURCE_DIR
#PACKET_X264=x264-master
#X264_OUTPUT_DIR=$PACKAGE_PREFIX/x264
#FFMPEG_EXTRA_LINK_LIBRARY="$FFMPEG_EXTRA_LINK_LIBRARY $X264_OUTPUT_DIR/lib/libx264.a"
#FFMPEG_EXTRA_CFLAGS="$FFMPEG_EXTRA_CFLAGS -I$X264_OUTPUT_DIR/include"
#FFMPEG_EXTRA_LDFLAGS="$FFMPEG_EXTRA_LDFLAGS -L$X264_OUTPUT_DIR/lib"
#FFMPEG_EXTRA_CONFIGURE_FLAGS="$FFMPEG_EXTRA_CONFIGURE_FLAGS --enable-encoder=libx264 --enable-libx264"
#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$X264_OUTPUT_DIR/lib/pkgconfig"
#if [ ! -e "$X264_OUTPUT_DIR/lib/libx264.a" ]; then
#  if [ ! -d "$PACKET_X264" ]; then
#    echo x264 Downloading...
#    wget https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
#    tar -xf $PACKET_X264.tar.bz2
#  fi
#  if [ "$ARCH" != "x64" ]; then
#    PACKET_X264_EXTRA_CONFIGURE_FLAG="--disable-asm"
#  fi
#  echo x264 Configuring...
#  pushd $PACKET_X264
#  ./configure \
#    --prefix=$X264_OUTPUT_DIR \
#    --enable-static \
#    --enable-pic \
#    --host=$ARCH-linux \
#    --disable-opencl \
#    --disable-cli \
#    --cross-prefix=$CROSS_PREFIX \
#    --sysroot=$SYSROOT \
#    $PACKET_X264_EXTRA_CONFIGURE_FLAG
#  echo x264 Building...
#  if [ -e "Makefile" ]; then
#    make clean
#  fi
#  make -j$(nproc) -s
#  echo x264 Installing...
#  make install
#  popd
#  popd
#fi
##x264 -end
#ffmpeg
pushd $PACKAGE_SOURCE_DIR
FFMPEG_SOURCE_DIR=$(pwd)/ffmpeg$FFMPEG_VERSION
if [ ! -d "$FFMPEG_SOURCE_DIR" ]; then
  echo ffmpeg Downloading...
  git clone -b n$FFMPEG_VERSION --depth 1 https://git.ffmpeg.org/ffmpeg.git $FFMPEG_SOURCE_DIR
fi
pushd $FFMPEG_SOURCE_DIR
build_ffmpeg() {
  #./configure -h
  # echo ffmpeg surport decoders...
  # ./configure --list-decoders
  # echo ffmpeg surport hwaccels...
  # ./configure --list-hwaccels
  echo ffmpeg Configuring...
  ./configure --target-os=android \
    --prefix=$PREFIX \
    --arch=armv8-a \
    --cpu=$CPU \
    --cc=$CC \
    --cxx=$CXX \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --enable-gpl \
    --cross-prefix=$CROSS_PREFIX \
    --enable-cross-compile \
    --enable-libx264 \
		--enable-encoder=libx264 \
    --enable-pthreads \
    --enable-jni \
    --enable-mediacodec \
    --enable-decoder=h264_mediacodec \
    --enable-decoder=hevc_mediacodec \
    --enable-decoder=vp8_mediacodec \
    --enable-decoder=vp9_mediacodec \
    --enable-decoder=mpeg2_mediacodec \
    --enable-decoder=mpeg4_mediacodec \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic -DVK_ENABLE_BETA_EXTENSIONS=0 -march=$CPU $FFMPEG_EXTRA_CFLAGS" \
    --extra-ldflags="$FFMPEG_EXTRA_LDFLAGS" \
    --pkg-config="pkg-config --static" \
    $SHARED_OR_STATIC_FLAG \
    $FFMPEG_EXTRA_CONFIGURE_FLAGS
  echo ffmpeg Building...
  if [ -e "Makefile" ]; then
    make clean
  fi
  make -j16
  echo ffmpeg Installing...
  make install
}
# SHARED_OR_STATIC_FLAG="--disable-shared --enable-static"
# echo "ffmpeg build static library"
# build_ffmpeg
SHARED_OR_STATIC_FLAG="--enable-shared  --disable-static"
echo "ffmpeg build shared library"
build_ffmpeg
$CC -shared -fPIC \
  $(find $FFMPEG_SOURCE_DIR/fftools -name '*.c' -and -not -name 'ffplay.c' -and -not -name 'ffprobe.c' -and -not -name 'ffplay_renderer.c' -and -not -name 'ffmpeg_qsv.c' -and -not -name 'ffmpeg_videotoolbox.c') \
  -I$FFMPEG_SOURCE_DIR -lm \
  $(find $PREFIX/lib -name "*.so" -and -not -name 'libffmpeg.so' -type f -print) \
  -o $PREFIX/lib/libffmpeg.so
#mv -v $PREFIX/lib/*.so $PREFIX
# pushd $PREFIX/lib
# for file in *.so; do
#   ln -s "$PREFIX/lib/$file" "$PREFIX/$file"
# done
# popd
popd
popd
echo "Generated files in: $PREFIX"
#ffmpeg -end
